package com.industry.system.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.industry.framework.common.Result;
import com.industry.framework.core.annountions.monitor.IdooMonitorController;
import com.industry.framework.core.generic.AbstractCoreGenericController;
import com.industry.system.controller.vo.MenuRespVO;
import com.industry.system.controller.vo.MenuTreeRespVO;
import com.industry.system.convert.MenuConvert;
import com.industry.system.dal.dataobject.SystemMenu;
import com.industry.system.dal.db.AuthBizMapper;
import com.industry.system.service.SystemMenuService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.industry.framework.common.Result.fail;
import static com.industry.framework.common.Result.success;

@RestController
@RequestMapping("/menu")
public class SystemMenuController extends AbstractCoreGenericController<SystemMenu, String> {

    @Resource
    private SystemMenuService service;
    @Resource
    private AuthBizMapper authBizMapper;
    @Value("${key.private}")
    private String privateKey;
    @Value("${key.public}")
    private String publicKey;

    @GetMapping("/hello")
    public Result<List<String>> hello() {
        // 使用公钥加密
        String ciphertext = SaSecureUtil.rsaEncryptByPublic(publicKey, "123456");
        System.out.println("公钥加密后：" + ciphertext);

        // 使用私钥解密
        String text2 = SaSecureUtil.rsaDecryptByPrivate(privateKey, ciphertext);
        System.out.println("私钥解密后：" + text2);
        IPage<Integer> menuPermissionPage = authBizMapper.getMenuPermissionPage(new Page<>(), 1L);
        return success(new ArrayList<>());
    }

    @GetMapping("roots")
    public Result<List<MenuRespVO>> listRootMenus() {
        List<SystemMenu> menus = service.listRootMenus();
        return success(MenuConvert.INSTANCE.convertList(menus));
    }

    @GetMapping("menu-setting-modules")
    public Result<List<SystemMenu>> getMenusByModules() {
        return success(service.getAllModules());
    }

    @GetMapping("query-menu-list-by-moduleId")
    public Result<List<MenuTreeRespVO>> queryMenusByModuleId(@RequestParam("moduleId") String moduleId) {
        return success(service.queryMenusByModuleId(moduleId));
    }

    @GetMapping("query-all-menu-tree")
    @IdooMonitorController("查询菜单树")
    public Result<List<MenuTreeRespVO>> listTreeMenus() {
        return success(service.listTreeMenus());
    }

    @GetMapping("query-menu-byId")
    @IdooMonitorController("根据Id查询菜单数据")
    public Result<SystemMenu> queryMenuById(@RequestParam("menuId") String menuId) {
        return success(service.queryMenuById(menuId));
    }

    //新增角色
    @PostMapping("insert")
    @IdooMonitorController("新增角色")
    public Result<Object> insert(@RequestBody SystemMenu systemMenu) {
        return service.insert(systemMenu) ? success("success") : fail("fail");
    }

    //修改角色
//    @PostMapping("modify")
//    public Result<String> modify(@RequestBody SystemMenu systemMenu){
//        service.modify(systemMenu);
//        return success();
//    }

    @GetMapping("query-setting-menu-type-config")
    public Result<List<SystemMenu>> querySettingMenuTypeConfig(@RequestParam("moduleKey") String moduleKey
            , @RequestParam("type") String type) {
        return success(service.queryMenuConfigSelect(moduleKey, type));
    }
}
